from datetime import datetime
from typing import List, Union, Literal, Optional

from sqlmodel import SQLModel, Field

from app.models.base import TimestampMixin, EmptyStrToNone, PageParams


class SystemAdmin(TimestampMixin, table=True):
    __tablename__ = "system_admin"

    id: int = Field(default=None, primary_key=True, index=True, nullable=False)
    role_ids: str = Field(default='', max_length=200, nullable=False)
    dept_ids: str = Field(default='', max_length=200, nullable=False)
    post_ids: str = Field(default='', max_length=200, nullable=False)
    username: str = Field(default='', max_length=32, nullable=False)
    nickname: str = Field(default='', max_length=32, nullable=False)
    password: str = Field(default='', max_length=200, nullable=False)
    is_super: int = Field(default=0, nullable=False)
    avatar: str = Field(default='', max_length=200, nullable=False)
    salt: str = Field(default='', max_length=20, nullable=False)
    sort: int = Field(default=0, nullable=False)
    is_multipoint: int = Field(default=0, nullable=False)
    is_disable: int = Field(default=0, nullable=False)
    last_login_ip: str = Field(default='', max_length=20, nullable=False)
    last_login_time: datetime = Field(default=None, nullable=True)
    delete_time: datetime = Field(default=None, nullable=True)


class SystemLoginIn(SQLModel):
    """系统登录参数"""
    username: str = Field(min_length=2, max_length=20)  # 账号
    password: str = Field(min_length=6, max_length=32)  # 密码


class SystemLoginOut(SQLModel):
    """系统登录返回信息"""
    token: str  # 令牌


class SystemLogoutIn(SQLModel):
    """退出登录参数"""
    token: str  # 令牌


class SystemAdminListIn(PageParams):
    """管理员列表参数"""
    username: Union[str, None] = Field(default=None)  # 账号
    nickname: Union[str, None] = Field(default=None)  # 昵称
    role: Union[int, None, EmptyStrToNone] = Field(default=None)  # 角色ID


class SystemAdminCreateIn(SQLModel):
    """管理员新增参数"""
    role_ids: List[int] = Field(alias='roleIds')  # 角色ID
    dept_ids: List[int] = Field(alias='deptIds')  # 部门ID
    post_ids: List[int] = Field(alias='postIds')  # 岗位ID
    username: str = Field(min_length=2, max_length=20)  # 账号
    nickname: str = Field(min_length=2, max_length=30)  # 昵称
    password: str  # 密码
    avatar: str  # 头像
    sort: int = Field(ge=0)  # 排序
    is_disable: int = Field(alias='isDisable', ge=0, le=1)  # 是否禁用: [0=否, 1=是]
    is_multipoint: int = Field(alias='isMultipoint', ge=0, le=1)  # 多端登录: [0=否, 1=是]


class SystemAdminEditIn(SystemAdminCreateIn):
    """管理员编辑参数"""
    id: int = Field(gt=0)  # 主键


class SystemAdminOut(SQLModel):
    """管理员返回信息"""
    id: int  # 主键
    username: str  # 账号
    nickname: str  # 昵称
    avatar: str  # 头像
    role: Union[str, None]  # 角色
    dept: Union[str, None]  # 部门
    isMultipoint: int = Field(alias='is_multipoint')  # 多端登录: [0=否, 1=是]
    isDisable: int = Field(alias='is_disable')  # 是否禁用: [0=否, 1=是]
    lastLoginIp: str = Field(alias='last_login_ip')  # 最后登录IP
    lastLoginTime: datetime  # 最后登录时间
    createTime: datetime  # 创建时间
    updateTime: Optional[datetime] = None  # 更新时间
